#!/bin/bash
set -e

if [ $# -eq 2 -a "x${2}" = "xsubbuild" ]; then
	echo "配置环境变量: [已配置]"
else
	echo "配置环境变量: [开始]"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------
	echo "配置环境变量: [完毕]"
fi

#-----------------------------------------------------------------------
export MODPKG_ROOT=${MIDKIT_TOPDIR}/pkg
export MODSRC_ROOT=${MIDKIT_TOPDIR}/src

export MODNAME=lzo
export MODPKG_NAME=lzo-2.10.tar.gz
export MODSRC_NAME=lzo-2.10

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=${MIDKIT_TOPDIR}/out/z.output.${MODNAME}

export MODREF_01_PATH=${MIDKIT_TOPDIR}/out/z.output.e2fsprogs
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function build_module()
{
	if [ ! -d ${MODOUT_PATH} ]; then
		mkdir -p ${MODOUT_PATH}
	fi

	if [ ! -f ${MODSRC_PATH}/Makefile ]; then
		sinfo "配置开始: [开始]"
		(cd ${MODSRC_PATH} && ./configure  --host=${HOST} --prefix=${MODOUT_PATH})
	    [ $? -ne 0 ] && serro "配置开始: [开始]" && exit 1
		pdone "编译开始: [完成" 
	fi

	mkdir -p ${MODOUT_PATH}/include/uuid
	mkdir -p ${MODOUT_PATH}/include/blkid

	cp -rf  ${MODREF_01_PATH}/include/uuid/*  ${MODOUT_PATH}/include/uuid/
	cp -rf  ${MODREF_01_PATH}/include/blkid/*  ${MODOUT_PATH}/include/blkid/

	(cd ${MODSRC_PATH} && make )
    [ $? -ne 0 ] && serro "编译开始: [失败]" && exit 1
	sdone "编译模块: [完成]"

	sinfo "安装模块: [开始]"
	(cd ${MODSRC_PATH} && make install)
    [ $? -ne 0 ] && serro "安装模块: [失败]" && exit 1
	sdone "安装模块: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function clean_module()
{
	sinfo "清除模块: [开始]"
	if [ -f ${MODSRC_PATH}/Makefile ]; then
		(cd ${MODSRC_PATH} && make clean)
		[ $? -ne 0 ] && serro "清除模块: [失败]"	&& exit 1
	fi
	sdone "清除模块: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function mainproc() 
{
	if [ "x$1" = "xbuild" ]; then
		check_env
		check_source
		export PKG_CONFIG_PATH=${MODOUT_PATH}/lib/pkgconfig
		export CFLAGS="-I${MODOUT_PATH}/include" 
		export LDFLAGS="-L${MODOUT_PATH}/lib"
		build_module
	elif [ "x$1" = "xclean" ]; then
		check_env
		clean_module
	elif [ "x$1" = "xpack" ]; then
		pack_module
	else
		sinfo "输入: $0 build        ${MODNAME}源码编译"
		sinfo "输入: $0 clean        ${MODNAME}清除编译"
		sinfo "输入: $0 pack         ${MODNAME}源码打包"
	fi
}
mainproc $1
